package com.ejlchina.tool.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.adolf.androiddemo.databinding.FragmentFullAndDistanceBinding
import com.huyuhui.fastble.data.BleDevice
import com.ejlchina.tool.adapter.DeviceAdapterRadio
import com.ejlchina.tool.adapter.pageadapter.FullAndDistancePagerAdapter
import com.ejlchina.tool.adapter.pageadapter.WeightCalibrationPagerAdapter
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.bean.FullAndDistanceBean
import com.ejlchina.tool.fragment.viewpager.FullAndDistancePagerFragment
import com.ejlchina.tool.fragment.viewpager.WeightCalibrationPagerFragment
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.singleton.GlobalParams
import com.ejlchina.tool.util.COMMAND_FULL_AND_DISTANCE_GET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_FULL_AND_DISTANCE_GET_UPWARD
import com.ejlchina.tool.util.COMMAND_FULL_AND_DISTANCE_SET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_FULL_AND_DISTANCE_SET_UPWARD
import com.ejlchina.tool.util.LOG_TAG
import com.ejlchina.tool.util.bytearray.intTo32HexStringLE
import com.ejlchina.tool.util.bytearray.readInt32LE
import com.ejlchina.tool.util.bytearray.toHexString
import com.ejlchina.tool.util.showToast
import com.ejlchina.tool.view.GridItemDecoration
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class FullAndDistanceFragment: BaseBindingFragment2<FragmentFullAndDistanceBinding>() {

    private val fullAndDistanceBean = FullAndDistanceBean()
    private val fragmentList = mutableListOf<FullAndDistancePagerFragment>()
    private var pageAdapter :FullAndDistancePagerAdapter? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initData()
    }

    private fun initViews(){
        setHeadTitle("激光测距")
    }

    private fun initData(){
        initTabAndViewPager()
    }

    private fun initTabAndViewPager(){
        binding.tabLayout.tabMode = TabLayout.MODE_SCROLLABLE
        fragmentList.clear()
        fragmentList.addAll(GlobalConnectObject.getCurrentConnectDeviceList().map { FullAndDistancePagerFragment(it) })

        pageAdapter = FullAndDistancePagerAdapter(this,fragmentList)
        binding.viewPager.adapter = pageAdapter
        binding.viewPager.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        TabLayoutMediator(
            binding.tabLayout, binding.viewPager
        ) { tab, position -> tab.setText("${GlobalConnectObject.getCurrentConnectDeviceList()[position].bucketNo}#") }.attach()
    }

    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentFullAndDistanceBinding {
        return FragmentFullAndDistanceBinding.inflate(inflater,container,true)
    }
}